/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package messageexample;

import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 *
 * @author v
 */
public class Main {
    @Resource(mappedName = "jms/messagesFactory")
    private static ConnectionFactory connectionFactory;
    @Resource(mappedName = "jms/messages")
    private static Queue queue;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Connection connection = null;
        Session session = null;
        MessageProducer producer = null;
        TextMessage messageOut = null;
        final int NUM_MSGS = 3;

        try {
            connection = connectionFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            producer = session.createProducer(queue);
            messageOut = session.createTextMessage();

            for (int i = 1; i <= NUM_MSGS; i ++) {
                messageOut.setText("This is a message number " + i);
                          System.out.println("Sending message: " + messageOut.getText());
                producer.send(messageOut);
            }

            System.out.println("To see if the bean received the messages,");
            System.out.println(
                    " check <install_dir>/domains/domain1/logs/server.log.");

        } catch (JMSException e) {
            System.out.println("Exception occurred: " + e.toString());
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                }
            } 

            System.exit(0);
        } 


    }



}
